package com.example.config;

import com.example.intercepter.LoginIntercepter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

//代替springMVC的配置文件
//1.扫描组件
//2.视图解析器
//3.view-controller
//4.default-servlet-handler
//5.mvc注解驱动
//6.文件上传解析器
//7.异常处理器
//8.拦截器
@Configuration
//扫描组件
@ComponentScan("com.example.controller")
@EnableWebMvc
public class SpringMvcConfig implements WebMvcConfigurer {


    //文件上传解析器
    @Bean("multipartResolver")
    public CommonsMultipartResolver getCommonsMultipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setDefaultEncoding("utf-8");
        multipartResolver.setResolveLazily(true);
        return multipartResolver;
    }

    //拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginIntercepter())
                .addPathPatterns("/**")
                .excludePathPatterns("/login","/upload","/download");
    }
}
